local socket = require ("socket")
local function GetAdd(hostname)
    local ip, resolved = socket.dns.toip(hostname)
    local ListTab = {}
    for k, v in ipairs(resolved.ip) do
        table.insert(ListTab, v)
    end
    return ListTab
end
local hostname = GetAdd(socket.dns.gethostname())



function net:clientInit()
    
    self.client = sock.newClient(SERVER_IP or hostname[2] or hostname[1],SERVER_PORT or 22122) 
    --self.client:setSendMode("unreliable")
    self.client:connect()

    self.connectTimer = 0
    self.ping = 0
    self.id = self.client.connectId
    self.client:on("connect", function(data)
        self.connected = true
        self.pingtimer = socket.gettime()
        self.client:send("ping")
        print("connected to server "..(SERVER_IP or hostname[2] or hostname[1]))
        if CLIENT_DEBUG then
            net.client:send("login",{id = "Alexar",password = "jinglei0"})
        end
    end)

    self.client:on("pong",function()
        self.ping = socket.gettime() - self.pingtimer
        
        delay:new(1,function()
            self.pingtimer = socket.gettime()
            if self.client then
                self.client:send("ping")
            end
        end)
    end)

    self.client:on("register_ok",function(data)
        local c = gamestate.current()
        c:userReg(true,data)
    end)

    self.client:on("register_err",function()
        local c = gamestate.current()
        c:userReg(false)
    end)


    self.client:on("login_ok",function(data)
        local c = gameState.login
        c:userLogin(true,data)
    end)

    self.client:on("login_err",function()
        local c = gamestate.current()
        c:userLogin(false)
    end)

    self.client:on("disconnect", function(data)
        self.connected = false
        user.id = nil
        print("dropped from server")
        gamestate.switch(gameState.title)
        delay:new(0.5,function()
            net:clientInit()
        end)
    end)

    self.client:on("restart_server", function(data)
        self.connected = false
        user.id = nil
        print("server restarted")
        gamestate.switch(gameState.title)
        delay:new(0.5,function()
            net:clientInit()
        end)
    end)


    self.client:on("your_roomlist",function(data)
        local c = gamestate.current()
        if c == gameState.room_select then
            c:setRoomList(data)
        end
    end)

    self.client:on("your_room",function(data)
        local c = gamestate.current()
        if c~=gameState.netgame then
            c:setWaiting(data)
        end
    end)

    self.client:on("game_start",function(data)
        gamestate.switch(gameState.netgame,data.id)
    end)

    self.client:on("reply_init",function(data)
        self:createWorld(data)      
    end)

    self.client:on("your_score",function(data)
        local  game = self:getGame(data)
        if not game then return end
        for id,s in pairs(data) do
            game.players[id].score = s
        end
    end)

    self.client:on("game_over",function(data)
        local  game = self:getGame(data)
        if not game then return end
        sound:stopAll()
        self.inGame = false
        gamestate.switch(gameState.result,data)
    end)


    self.client:on("sync_game_time",function(data)
        local c = gamestate.current()
        c.game.gameTime = data.time
    end)

    self.client:on("create_obj",function(data)
        self:createObj(data)
    end)
    
    self.client:on("move_obj",function(data)
        self:moveObj(data)
    end)

    self.client:on("kill_obj",function(data)
        local  game = self:getGame(data)
        if not game then return end
        if game.objects[data.id] then
            game.objects[data.id]:destroy()
        end
    end)
    
    self.client:on("set_tile",function(data)
        local  game = self:getGame(data)
        if not game then return end
        game.map:setTile(unpack(data))
    end)

    self.client:on("your_tank",function(data)
        local  game = self:getGame(data)
        if not game then return end
        game:setPlayer(game.objects[data.id],data.id)
    end)


    self.client:on("take_shell",function(data)
        local  game = self:getGame(data)
        if not game then return end
        sound:play("itemget")
        local tank = game.objects[data.id]
        tank:takeShell(data.t)
    end)

    self.client:on("power_off",function(data)
        local  game = self:getGame(data)
        if not game then return end
        sound:play("itemget")
        local tank = game.objects[data.id]
        tank:poweroff(data.t)
    end)

    self.client:on("boom",function(data)
        local  game = self:getGame(data)
        if not game then return end
        sound:play("itemget")
        --game.objects[data.id]:damage()
    end)

    self.client:on("hq_destroyed",function(data)
        local  game = self:getGame(data)
        if not game then return end
        sound:play("destroyme")
        local hq = game.objects[data.id]
        hq.fallen = true
    end)

    self.client:on("change_type",function(data)
        local  game = self:getGame(data)
        if not game then return end
        sound:play("levelup")
        game.objects[data.id]:changeType(data.t)
    end)

    self.client:on("get_item",function(data)
        local  game = self:getGame(data)
        if not game then return end
        sound:play("itemget")
    end)
    self.client:on("damage",function(data)
        local  game = self:getGame(data)
        if not game then return end
        sound:play("touchiron")
    end)
end